package com.yksj.healthtalk.ui.chatting;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.dictionary.DictionaryContentActivity;
import com.yksj.healthtalk.ui.server.ServerShopSeachActivity;

public class ChatMedicinalListActivity extends BaseFragmentActivity implements OnClickListener {
	String mTitle;//title
	String mCode;//编码
	ListView mListView;
	DataBaseAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.chat_madicinal_list_layout);
		
		Intent intent = getIntent();
		mCode = intent.getStringExtra("code");
		mTitle = intent.getStringExtra("title");
		
		initView();
		
		onQueryData();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
			//药品中心
		case R.id.dictionary_filter1:
			Intent intent = new Intent(this,ServerShopSeachActivity.class);
			intent.putExtra("code",mCode);
			startActivity(intent);
			break;
		}
	}
	
	private void initView(){
		initTitle();
		
		setTitle(mTitle);
		
		setBackBtn("返回",this);
		
		mListView = (ListView)findViewById(android.R.id.list);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				position--;
				JSONObject object = mAdapter.getItem(position);
				onQueryContentData(mAdapter.getCode(object), mAdapter.getName(object));
			}
		});
	}
	
	/**
	 * 查询列表数据
	 */
	private void onQueryData(){
		HttpRestClient.doHttpQueryMedicinesNewServlet("1",mCode,new JsonHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONArray response) {
				if(response != null){
					mAdapter = new DataBaseAdapter(response);
					View view = getLayoutInflater().inflate(R.layout.button_layout,null);
					view.findViewById(R.id.dictionary_filter1).setVisibility(View.GONE);
//					Button button = (Button)view.findViewById(R.id.dictionary_filter1);
//					button.setText("去药品中心");
//					button.setOnClickListener(ChatMedicinalListActivity.this);
					mListView.addHeaderView(view,null,false);
					mListView.setAdapter(mAdapter);
				}
			}
		});
	}
	
	/**
	 * 根据item查询内容
	 * @param code
	 * @param name
	 */
	private void onQueryContentData(String code,String name){
		HttpRestClient.doHttpQueryMedicinesServlet("4", null, code, null, null, new MHttpResponseHandler(name,code));
	}
	
	private class MHttpResponseHandler extends AsyncHttpResponseHandler{
		private String mTitleName;
		private String mCode;
		public MHttpResponseHandler(String name,String code) {
			super(ChatMedicinalListActivity.this);
			this.mTitleName = name;
			this.mCode = code;
		}
		public void onSuccess(int statusCode, String content) {
			if(!isFinishing()){
				Intent intent = new Intent(ChatMedicinalListActivity.this,DictionaryContentActivity.class);
				intent.putExtra("result",content);
				intent.putExtra("title",mTitleName);
				startActivityForResult(intent,1);	
			}
		};
	}
	
	class DataBaseAdapter extends BaseAdapter{
		JSONArray mJsonArray;
		
		public DataBaseAdapter(JSONArray array) {
			mJsonArray = array;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mJsonArray.length();
		}

		@Override
		public JSONObject getItem(int position) {
			// TODO Auto-generated method stub
			return mJsonArray.optJSONObject(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		
		private String getCode(JSONObject jsonObject){
			return jsonObject.optString("CODE");
		}
		
		private String getName(JSONObject jsonObject){
			return jsonObject.optString("NAME");
		}
		
		private String getAddress(JSONObject jsonObject){
			return  jsonObject.optString("STANDARDS");
		}
		private String getLotNumber(JSONObject jsonObject){
			return  jsonObject.optString("LOT_NUMBER");
		}

		private String getFactory(JSONObject jsonObject){
			return jsonObject.optString("FACTORY");
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = getLayoutInflater().inflate(R.layout.item_chat_medicainal_layout,null);
				viewHolder.textView1 = (TextView)convertView.findViewById(R.id.text1);
				viewHolder.textView2 = (TextView)convertView.findViewById(R.id.text2);
				viewHolder.textView3 = (TextView)convertView.findViewById(R.id.text3);
				viewHolder.textView4 = (TextView)convertView.findViewById(R.id.text4);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder)convertView.getTag();
			}
			JSONObject jsonObject = getItem(position);
			viewHolder.textView1.setText(getName(jsonObject));
			viewHolder.textView2.setText(getAddress(jsonObject));
			viewHolder.textView3.setText(getFactory(jsonObject));
			viewHolder.textView4.setText(getLotNumber(jsonObject));
			return convertView;
		}
	}
	
	private static class ViewHolder{
		TextView textView1;
		TextView textView2;
		TextView textView3;
		TextView textView4;
	}

	
	
}
